package max.ubitest.pipe.view
{
	import flash.events.MouseEvent;
	
	import max.ubitest.pipe.data.CellValidation;
	import max.ubitest.pipe.data.ComponentData;
	import max.ubitest.pipe.data.ComponentTypes;
	import max.ubitest.pipe.events.PipeEvent;
	import max.ubitest.pipe.locators.ComponentsLocator;
	
	import mx.core.IVisualElementContainer;
	
	import org.robotlegs.mvcs.Mediator;
	
	public class ToolBarMediator extends Mediator
	{
		[Inject]
		public var view:ToolBar;
		[Inject]
		public var locator:ComponentsLocator;
		
		public function ToolBarMediator()
		{
			super();
		}
		
		override public function onRegister():void{
			eventMap.mapListener(view.btDel, MouseEvent.CLICK, onDel);
			eventMap.mapListener(view.btMove, MouseEvent.CLICK, onMove);
			eventMap.mapListener(view.btRotate, MouseEvent.CLICK, onRotate);
			eventMap.mapListener(eventDispatcher, PipeEvent.RESET, onReset);
			
			init();
		}
		
		override public function preRemove():void{
			eventMap.unmapListener(view.btDel, MouseEvent.CLICK, onDel);
			eventMap.unmapListener(view.btMove, MouseEvent.CLICK, onMove);
			eventMap.unmapListener(view.btRotate, MouseEvent.CLICK, onRotate);
			eventMap.unmapListener(eventDispatcher, PipeEvent.RESET, onReset);
		}
		
		private function addBack(cd:ComponentData):void{
			for(var i:int = 0;i<locator.components.length;i++){
				if(locator.components.getItemAt(i).type == cd.type){
					locator.components.getItemAt(i).amount ++;
				}
			}
		}
		
		private function onReset(e:PipeEvent):void{
			dispose();
		}
		
		private function onDel(e:MouseEvent):void{
			//find component data from locator.mappedComponents and remove it
			for(var i:int = 0;i<locator.mappedComponents.length;i++){
				if(locator.mappedComponents.getItemAt(i).componentData == locator.edittingComponent.data){
					locator.mappedComponents.getItemAt(i).valid = CellValidation.NONE_FITTING;
					locator.mappedComponents.removeItemAt(i);
					addBack(locator.edittingComponent.data as ComponentData);
					locator.reset();
					dispose();
					
					var validEvent:PipeEvent = new PipeEvent(PipeEvent.DO_VALIDATION, null);
					dispatch(validEvent);
					break;
				}
			}
		}
		
		private function onMove(e:MouseEvent):void{
			//unselect selected component
			dispatch(new PipeEvent(PipeEvent.COMPONENT_UNSELECTED, null));
			
			for(var i:int = 0;i<locator.mappedComponents.length;i++){
				if(locator.mappedComponents.getItemAt(i).componentData == locator.edittingComponent.data){
					locator.mappedComponents.removeItemAt(i);
					locator.selectedComponent = locator.edittingComponent.data as ComponentData;
					locator.reset();
					dispose();
					
					var validEvent:PipeEvent = new PipeEvent(PipeEvent.DO_VALIDATION, null);
					dispatch(validEvent);
					break;
				}
			}
		}
		
		private function onRotate(e:MouseEvent):void{
			locator.edittingComponent.data.rotate();
			
			var validEvent:PipeEvent = new PipeEvent(PipeEvent.DO_VALIDATION, null);
			dispatch(validEvent);
		}
		
		private function init():void{
			var cr:ComponentRenderer = locator.edittingComponent;
			if(cr == null){
				dispose();
				return;
			}
			view.x = cr.x + cr.width + 10;
			view.y = cr.y;
		}
		
		private function dispose():void{
			if(view.parent){
				(view.parent as IVisualElementContainer).removeElement(view);
			}
		}
	}
}